<template>
  <div ref="mainCon" class="container">
    <topTitle v-if="!hiddleToolBar" :titleText="titleText"></topTitle>
    <div class="mainCon">
      <el-row class="toolbar searchInputWidth" style="padding-bottom: 0">
        <el-form :inline="true" :model="filterField" ref="searchConditions">
          <el-col :lg="8" :xl="6">
            <el-form-item label="人员姓名" class="row-padding-bottom">
              <el-input
                placeholder="请输入姓名或身份证号"
                clearable
                v-model="filterField.keyword"
                @blur="filterField.keyword = changeValue($event)"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="8" :xl="6">
            <el-form-item label="证书编号" class="row-padding-bottom">
              <el-input
                placeholder="请输入证书编号"
                clearable
                v-model="filterField.certificateNumber"
                @blur="filterField.certificateNumber = changeValue($event)"
              ></el-input>
            </el-form-item>
          </el-col>
          <template v-if="advanced">
            <el-col :lg="8" :xl="6">
              <el-form-item label="审核状态" class="row-padding-bottom">
                <el-select
                  v-model="filterField.auditStatus"
                  filterable
                  placeholder="请选择"
                  clearable
                  :popper-append-to-body="false"
                >
                  <el-option
                    v-for="item in statusOptions"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </template>
          <el-form-item label="">
            <a
              @click="toggleAdvanced"
              style="margin-left: 40px; color: #409eff; cursor: pointer"
              class="btnColor"
            >
              {{ advanced ? '收起' : '展开' }}
              <i
                :class="advanced ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"
              ></i>
            </a>
            <el-button
              type="primary"
              icon="icon iconfont iconchazhao1"
              @click="searchList"
              class="searchBtn"
            >
              <span>搜索</span>
            </el-button>
            <!-- <el-button class="commonBut saveBtn funComBut" icon="icon iconfont icondaochu" v-sdc @click="exportList">
                            <span class="opetareText">导出</span>
                        </el-button>
                        <el-button class="commonBut saveBtn funComBut" icon="icon iconfont icontianjia" @click="toAddLabor">
                            <span class="opetareText">添加劳资资质</span>
                        </el-button> -->
          </el-form-item>
        </el-form>
      </el-row>
      <div class="spacing"></div>
      <div
        ref="tableBox"
        :class="hiddleToolBar ? 'tableBoxCur tableBox' : 'tableBoxCur'"
      >
        <div style="margin-bottom: 24px">
          <!-- <el-button class="commonBut saveBtn funComBut" icon="icon iconfont icondaochu" v-sdc @click="exportList">
                        <span class="opetareText">导出</span>
                    </el-button> -->
          <exportBtn
            :dataLen="tableData.length"
            :filterField="filterField"
            :responseType="`get`"
            api="api-e/cs/qualification/expand/export"
          ></exportBtn>
          <el-button
            class="commonBut saveBtn funComBut"
            icon="icon iconfont icontianjia"
            @click="toAddLabor"
          >
            <span class="opetareText">添加劳资资质</span>
          </el-button>
          <el-button
            class="commonBut saveBtn funComBut"
            @click="toAuditBatch('1')"
          >
            <span class="opetareText">批量审核</span>
          </el-button>
          <el-button
            class="commonBut funComBut rejectFrom"
            @click="toAuditBatch('2')"
          >
            <span class="opetareText">批量驳回</span>
          </el-button>
        </div>
        <!-- 列表 -->
        <el-table
          ref="tableBox1"
          :max-height="tableHeight"
          :data="tableData"
          :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
          highlight-current-row
          stripe
          v-loading="loading"
          style="border: 1px solid #dfe6ec"
          @sort-change="sortChange"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column
            align="center"
            prop="imgPath"
            label="身份证照片"
            width="120"
          >
            <template slot-scope="scope">
              <el-image
                v-if="scope.row.imgPath"
                style="width: 50px; height: 50px; top: 3.5px"
                :src="scope.row.imgPath"
                :preview-src-list="scope.row.idcardImgList"
              >
              </el-image>
              <el-image
                v-else
                src="../../../static/img/employee/card.png"
                alt=""
                style="width: 50px; height: 50px; top: 3.5px"
              />
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="name"
            label="姓名"
            min-width="120"
            :formatter="formatTd"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="idcard"
            label="身份证号"
            min-width="120"
          >
            <template slot-scope="scope">
              <p>{{ scope.row.idcard | deseHandler(4, 4, '*') || '--' }}</p>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="certificateNumber"
            label="证书编号"
            width="150"
            :formatter="formatTd"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="sexValue"
            label="性别"
            :formatter="formatTd"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="nationalityValue"
            label="民族"
            :formatter="formatTd"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="createTime"
            label="录入时间"
            sortable="custom"
            :formatter="formatTd"
            width="120"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="auditStatusStr"
            label="审核状态"
            :formatter="formatTd"
            min-width="140"
          ></el-table-column>
          <el-table-column align="left" label="操作" fixed="right" width="200">
            <!-- 操作：待审核-审核和人员档案   已驳回/已通过  查看详情和人员档案 -->
            <template slot-scope="scope">
              <div v-if="scope.row.auditStatusStr == '待审核'">
                <el-link type="primary" @click="toAudit(scope.row)"
                  >审核</el-link
                >
                <el-link type="primary" @click="toEmployeeDetail(scope.row)"
                  >人员档案</el-link
                >
              </div>
              <div v-else>
                <el-link type="primary" @click="toDetail(scope.row)"
                  >查看详情</el-link
                >
                <el-link type="primary" @click="toEmployeeDetail(scope.row)"
                  >人员档案</el-link
                >
              </div>
            </template>
          </el-table-column>
          <!-- 表格数据为空时，页面显示 -->
          <div slot="empty" v-show="isShow">
            <emptyTable emptyText="暂无相关信息"></emptyTable>
          </div>
        </el-table>
        <!-- filterField为搜索条件 -->
        <Pagination
          ref="pagination"
          @getList="getTableList"
          :requestUrl="requestUrl"
          :filterField="filterField"
        ></Pagination>
      </div>
      <!-- 驳回弹框 -->
      <el-dialog
        title="驳回"
        :visible.sync="formVisibleReject"
        :close-on-click-modal="false"
        @close="closeDialog"
        custom-class="brand-dialog-min-width"
        width="420px"
        class="tipDialog"
        :modal-append-to-body="false"
        :append-to-body="true"
      >
        <el-form
          :model="formDataReject"
          label-width="120px"
          :rules="formRulesReject"
          ref="formDataReject"
        >
          <el-form-item
            label="驳回原因"
            class="row-padding-bottom"
            prop="reason"
          >
            <el-input
              type="textarea"
              rows="3"
              placeholder="请输入驳回原因，提交后将短信通知项目"
              v-model="formDataReject.reason"
              @blur="formDataReject.reason = changeValue($event)"
              maxlength="100"
              show-word-limit
            ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click.native="handleClose" class="cancel">取消</el-button>
          <el-button
            type="primary"
            @click.native="submitRejectSave"
            :loading="regectSubmit"
            v-sdc
            class="save"
            >保存</el-button
          >
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import common from '../../kits/common'
import scrollTable from '@/mixin/scrollTable.js'
import { mapActions, mapState } from 'vuex'

export default {
  name: 'laborInfoAuditCz',
  mixins: [scrollTable],
  data() {
    return {
      titleText: '劳资资质审核',
      advanced: false,
      statusOptions: [
        { name: '待审核', id: '0' },
        { name: '已通过', id: '1' },
        { name: '已驳回', id: '2' },
      ], //审核状态
      //表格数据
      tableData: [],
      requestUrl: 'api-e/cs/qualification/expand/list',
      idcardImgList: [],
      //搜索条件
      filterField: {
        keyword: '',
        certificateNumber: '',
        auditStatus: '',
      },
      isShow: false, //列表加载
      loading: true,
      isShowDetail: '',
      activedList: [],
      isLoading: false,
      formVisibleReject: false, //驳回弹框
      formDataReject: {
        reason: '',
      },
      formRulesReject: {
        reason: [
          {
            required: true,
            message: '请输入驳回原因',
            trigger: 'blur',
          },
        ],
      },
    }
  },
  created() {
    this.$nextTick(function () {
      this.$refs.pagination.getTableListData()
    })
  },
  methods: {
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    getTableList(data) {
      this.tableData = data
      this.loading = false
      if (this.tableData.length < 1) {
        this.isShow = true
      } else {
        this.idcardImgList = []
        for (var i = 0; i < this.tableData.length; i++) {
          this.idcardImgList.push(this.tableData[i].imgPath)
        }
        for (var i = 0; i < this.tableData.length; i++) {
          var container = this.idcardImgList
          var frontArr = container.slice(0, i)
          var behindArr = container.slice(i, this.tableData.length)
          var list = behindArr.concat(frontArr)
          this.tableData[i].idcardImgList = list
        }
      }
    },
    // 搜索
    searchList() {
      this.$refs.pagination.getTableListData()
    },
    // 导出
    // exportList() {
    //     if (this.tableData.length > 0) {
    //         window.location.href =
    //             common.commonApi +
    //             "api-e/cs/qualification/expand/export?keyword=" +
    //             this.filterField.keyword +
    //             "&certificateNumber=" +
    //             this.filterField.certificateNumber +
    //             "&auditStatus=" +
    //             this.filterField.auditStatus +
    //             "&orderBy=" +
    //             this.filterField.orderBy +
    //             "&access_token=" +
    //             localStorage.getItem("access_token");
    //     } else {
    //         this.$message({
    //             type: "error",
    //             message: "无数据导出"
    //         });
    //     }
    // },
    //有审核驳回的操作页
    toAudit(row) {
      this.$router.push({
        path: '/home/employee/laborAuditCz',
        name: 'laborAuditCz',
        query: {
          id: row.id,
        },
      })
      this.$store.commit('CHANGE_SLIDER_NAME', {
        oldUrl: '/home/employee/laborInfoAuditCz',
        newUrl: this.$router.currentRoute.fullPath,
      })
    },
    //查看详情
    toDetail(row) {
      this.$router.push({
        path: '/home/employee/laborAuditDetailCz',
        name: 'laborAuditDetailCz',
        query: {
          id: row.id,
        },
      })
      this.$store.commit('CHANGE_SLIDER_NAME', {
        oldUrl: '/home/employee/laborInfoAuditCz',
        newUrl: this.$router.currentRoute.fullPath,
      })
    },
    //人员档案页面
    toEmployeeDetail(row) {
      this.$router.push({
        path: '/home/employee/employeeInfoC',
        name: 'employeeInfoC',
        query: {
          employeeId: row.employeeId,
          projectId: row.projectId,
          type: 'laborInfoAuditCz',
        },
      })
      this.$store.commit('CHANGE_SLIDER_NAME', {
        oldUrl: '/home/employee/laborInfoAuditCz',
        newUrl: this.$router.currentRoute.fullPath,
      })
    },
    toAddLabor() {
      this.$router.push({
        path: '/home/employee/laborInfoAddCz',
        name: 'laborInfoAddCz',
        query: {
          path: 'laborInfoAuditCz',
        },
      })
      this.$store.commit('CHANGE_SLIDER_NAME', {
        oldUrl: '/home/employee/laborInfoAuditCz',
        newUrl: this.$router.currentRoute.fullPath,
      })
    },
    handleSelectionChange(val) {
      this.activedList = val
    },
    toAuditBatch(type) {
      if (this.activedList.length < 1) {
        this.$message({
          type: 'warning',
          message: '请勾选待审核的劳资专管员',
        })
        return
      }
      this.isLoading = true
      // 审核
      if (type == '1') {
        this.$confirm(
          '是否确认审核通过？通过审核后将展示到劳资专管员页面！',
          '提示',
          {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning',
          }
        )
          .then(() => {
            this.auditBatch(type)
          })
          .catch(() => {
            //取消
          })
        // this.auditBatch(type);
      } else {
        // 驳回
        this.formVisibleReject = true
        this.formDataReject.reason = ''
      }
    },
    submitRejectSave() {
      this.auditBatch('2')
    },
    handleClose() {
      this.formVisibleReject = false
      this.$refs['formDataReject'].resetFields()
    },
    auditBatch(type) {
      var arr = []
      this.activedList.forEach((item) => {
        arr.push(item.id)
      })
      var data = {
        idsJson: JSON.stringify(arr),
        auditStatus: type,
        rejectReason: this.formDataReject.reason,
      }
      this.axios
        .post('api-e/cs/qualification/expand/auditBatch', data)
        .then((res) => {
          if (!res.data.hasError) {
            this.$message({
              type: 'success',
              message: res.data.msg,
            })
            this.isLoading = false
            if (type == '2') {
              this.formVisibleReject = false
            }
            this.$nextTick(function () {
              this.$refs.pagination.getTableListData()
            })
          } else {
            this.$message({
              type: 'warning',
              message: res.data.errorMsg,
            })
            this.isLoading = false
          }
        })
    },
  },
}
</script>

<style lang="less" scoped>
@import '../../../static/css/filterArea.less';
.rejectFrom {
  background-color: #3e5265;
  border-color: #3e5265;
  color: #fff;
  &:hover {
    background-color: #3e5265;
    border-color: #3e5265;
    color: #fff;
    opacity: 0.8;
  }
  &:focus {
    background-color: #3e5265 !important;
    border-color: #3e5265 !important;
    color: #fff !important;
  }
}
</style>
